package me.yxy.mobanddd.controller;

import me.yxy.mobanddd.components.MPage;
import me.yxy.mobanddd.controller.request.query.BaseQueryRequest;
import me.yxy.mobanddd.controller.request.query.TransportPageRequest;
import me.yxy.mobanddd.controller.response.QueryResponse;
import me.yxy.mobanddd.domain.transportorder.TransportOrder;
import me.yxy.mobanddd.domain.warehouse.Warehouse;
import me.yxy.mobanddd.domain.warehouse.WarehouseRepository;
import me.yxy.mobanddd.finder.TransportOrderFinder;
import me.yxy.mobanddd.finder.WarehouseFinder;
import me.yxy.mobanddd.finder.view.transport.TransportConsoleView;
import me.yxy.mobanddd.finder.view.warehouse.WarehouseConsoleGroupView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;

/**
 * TODO
 * 纯查询报表Controller，其实某种意义上来说，这个Controller可以通过框架构建直接略掉，
 * 因为所有事务查询都会委派到Finder中，这里这需要做参数校验和格式转换，除非特殊的情况需要单独Override处理，其余大部分场景可以直接通过<b>配置</b>甚至<b>约定+反射</b>来解决，那样，这一层，几乎就是透明的了
 * <p></p>
 *
     PATH的命名方式：/query/{domain}/{application}（虽说不是强制，但强烈建议遵守，因为若不遵守可能会影响部分特性的使用)
  <ul>
    <li>/query/shipment/customer-app-list {用户APP}的{调度单列表}</li>
    <li>/query/transport/console-list {运营后台}的{运输单列表}</li>
 </ul>

 */
@SuppressWarnings("unused")
@RestController
@RequestMapping("/query")
@ResponseBody
public class QueryEntrance {

    /**
     * 运输单通用单表查询
     * @param request
     * @return
     */
    @PostMapping("/transport/simple-page")
    public QueryResponse transportPage(@RequestBody TransportPageRequest request){
        Pageable pageable = PageRequest.of(request.getPageNumber(), request.getPageSize());
        Page<TransportOrder> pageResult = transportOrderFinder.pageSimple(request,pageable);
        return QueryResponse.ok(MPage.of(pageResult));
    }

    /**
     *
     * 运营后台运输单聚合表查询
     * @param request
     * @return
     */
    @PostMapping("/transport/console-list")
    public QueryResponse transportViewPage(@RequestBody TransportPageRequest request){
        Pageable pageable = PageRequest.of(request.getPageNumber(), request.getPageSize());
        Page<TransportConsoleView> pageResult = transportOrderFinder.pageTransportView(request,pageable);
//        return QueryResponse.ok(pageResult);
        return QueryResponse.ok(MPage.of(pageResult));
    }


    /**
     *
     * 运营后台仓库每日订单汇总统计
     * @param request
     * @return
     */
    @PostMapping("/warehouse/console-group-list")
    public QueryResponse transportViewPage(@RequestBody BaseQueryRequest request){
        Pageable pageable = PageRequest.of(request.getPageNumber(), request.getPageSize());

        //TODO 还需要引入一下排序参数

        MPage<WarehouseConsoleGroupView> pageResult = warehouseFinder.countOrdersByWarehouse("2019-04-26",pageable);
        return QueryResponse.ok(pageResult);
    }


    /**
     * 仓库单个信息
     * @param warehouseId
     * @return
     */
    @GetMapping("/warehouse/{id}")
    public String getWarehouseInfo(@PathVariable("id") long warehouseId){
        Optional<Warehouse> warehouse = warehouseRepository.findById(warehouseId);
        return warehouse.orElse(null).getName();
    }

    @Autowired
    TransportOrderFinder transportOrderFinder;
    @Autowired
    WarehouseFinder warehouseFinder;

    @Autowired
    WarehouseRepository warehouseRepository;

    @PostMapping("/page/warehouse")
    public Page<Warehouse> warehousePage(@RequestBody TransportPageRequest request){
        Pageable pageable = PageRequest.of(request.getPageNumber(), request.getPageSize());
        Page<Warehouse> page =  warehouseRepository.findAll(pageable);
        return page;
    }

}


//controller - methodName
//Request-created by reflaction
//Response-created by reflaction

    /*
    @RequestMapping(value = "/params", method=RequestMethod.GET)
public Page<Blog> getEntryByParams(@RequestParam(value = "page", defaultValue = "0") Integer page,
        @RequestParam(value = "size", defaultValue = "15") Integer size) {
    Sort sort = new Sort(Direction.DESC, "id");
    Pageable pageable = new PageRequest(page, size, sort);
    return blogRepository.findAll(pageable);
     */
